let exports = {}
import { ImagePreview } from 'vant'
exports.install = function(Vue) {
  Vue.prototype.$sceneImg = function(images) {
    ImagePreview({
      images: [images], //需要预览的图片 URL 数组
      showIndex: false, //是否显示页码
      loop: false, //是否开启循环播放
    })
  }
  Vue.prototype.$encode = function(str) {
    // 对字符串进行编码
    var encode = encodeURI(str)
    // 对编码的字符串转化base64
    var base64 = btoa(encode)
    return base64
  }
  Vue.prototype.$decode = function(base64) {
    // 对base64转编码
    var decode = atob(base64)
    // 编码转字符串
    var str = decodeURI(decode)
    return str
  }
  Vue.prototype.$math = {
    // 乘法
    mul(arg1, arg2) {
      const r1 = arg1.toString()
      const r2 = arg2.toString()
      const d = arguments[2]
      const m = (r1.split('.')[1] ? r1.split('.')[1].length : 0) + (r2.split('.')[1] ? r2.split('.')[1].length : 0)
      const resultVal = Number(r1.replace('.', '')) * Number(r2.replace('.', '')) / Math.pow(10, m)
      return typeof d !== 'number' ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)))
    },
    div: function(arg1, arg2) {
      const r1 = arg1.toString()
      const r2 = arg2.toString()
      const d = arguments[2]
      const m = (r2.split('.')[1] ? r2.split('.')[1].length : 0) - (r1.split('.')[1] ? r1.split('.')[1].length : 0)
      const resultVal = Number(r1.replace('.', '')) / Number(r2.replace('.', '')) * Math.pow(10, m)
      return typeof d !== 'number' ? Number(resultVal) : Number(resultVal.toFixed(parseInt(d)))
    },
    add: function(arg1, arg2) {
      arg1 = arg1.toString()
      arg2 = arg2.toString()
      const arg1Arr = arg1.split('.')
      const arg2Arr = arg2.split('.')
      const d1 = arg1Arr.length === 2 ? arg1Arr[1] : ''
      const d2 = arg2Arr.length === 2 ? arg2Arr[1] : ''
      const maxLen = Math.max(d1.length, d2.length)
      const m = Math.pow(10, maxLen)
      const result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen))
      const d = arguments[2]
      return typeof d === 'number' ? Number((result).toFixed(d)) : result
    },
    sub: function(arg1, arg2) {
      return this.add(arg1, -Number(arg2), arguments[2])
    }
  }
}

export default exports
